package cn.com.pxto.web.ajax;

import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONException;
import org.json.JSONObject;

import cn.com.pxto.model.School;
import cn.com.pxto.service.SchoolManager;



public class AjaxAction extends Action {
	
	private SchoolManager schoolManager ;
	
	/**
	 * for �������û���Ȩʱ������ϼ��������
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String ltid = request.getParameter("lessonTypeId");
		List<School> list =schoolManager.getSchoolByLessonsType(ltid);
		JSONObject jo = new JSONObject();
		schoolListToJSONObject(jo,list);
		request.setAttribute("returnJo", jo);
		response.setContentType("text/xml;charset=gbk"); 
		PrintWriter out=response.getWriter();
        out.write(jo.toString());
        System.out.println(jo.toString());
        out.close();
		return null;

	/*	int id=ParamUtil.getIntParams(request, "deptId");
		DepartmentManager departmentManager = new DepartmentManager();
		String parentName=null;
		if(deptId==0){
			parentName="";
		}
		parentName=departmentManager.getParDeptName(deptId);
		//response.setHeader("Charset","gbk");
		response.setContentType("text/xml;charset=gbk"); 
		PrintWriter out=response.getWriter();
        out.write(parentName);
        out.close();
		return null;*/
	}
	
	private void schoolListToJSONObject(JSONObject jo , List<School> list){
		Iterator<School> it = list.iterator();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			School school = (School) iterator.next();
			try {
				jo.put(school.getSid().toString(),school.getName());
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void setSchoolManager(SchoolManager schoolManager) {
		this.schoolManager = schoolManager;
	}
	
}
